// pages/haibao/haibao.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgData: null,     //设置画板的显示与隐藏，画板不隐藏会影响页面正常显示
    webUrl: null,     //回跳到web页面
    goods_id: null,
    dealer_member_id: null,
    url: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      console.log(options)
      //接收上面的参数
      this.imgData = options.imgData;
      this.goods_id = options.goods_id;
      this.dealer_member_id = options.dealer_member_id;

      console.log(options.goods_id, options.dealer_member_id)
      //保存图片,保存方法和上门的一直
      this.download();
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  download() {
    let that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success(res) {
              that.saveImg()
            },
            fail(err) {
              console.log(err);
              
              if (err.errMsg != 'authorize:fail auth deny') {
                wx.showModal({
                  title: '授权提示',
                  content: '是否允许获取保存相册权限',
                  success: (res) => {
                    if (res.confirm) { // 点击确定，则调用相册授权
                      wx.openSetting({
                        success(settingdata) {
                          if (settingdata.authSetting["scope.writePhotosAlbum"]){
                            wx.showToast({
                              title: '授权成功'
                            });
                           
                          } else {
                            wx.showToast({
                              title: '请确定已打开保存权限',
                              icon: "none"
                            });
                          }
                          wx.navigateBack({ changed: true });
                        }
                      })
                    }else{
                      wx.navigateBack({ changed: true });
                    }
                  }
                })
              }else{
                wx.navigateBack({ changed: true });
              }
            }
          })
        } else {
          that.saveImg()
        }
      }
    })
  },

  saveImg() {
    var _this = this;
    var img = this.imgData; 
    wx.showLoading({ title: '正在下载中...' });
    wx.downloadFile({
      url: img,　　　　　　　//需要下载的图片url
      success: function (res) {　　　　　　　　　　　　//成功后的回调函数
        wx.saveImageToPhotosAlbum({　　　　　　　　　//保存到本地
          filePath: res.tempFilePath,
          success(res) {
            
            wx.showToast({
              title: '保存成功',
              duration: 1000,
              //  icon: 'none',
              icon: 'success',
              success: function () {

                // _this.url =  `${getApp().globalData.webUrl}/wa/distribution/bangmai.html?goods_id=${_this.goods_id}&dealer_member_id=${_this.dealer_member_id}`;
                setTimeout(function () {
                  wx.navigateBack({ changed: true });
                }, 1500)

              }

            });
          },
          fail: function (err) {
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              wx.openSetting({
                success(settingdata) {
                  console.log(settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    console.log('获取权限成功，再次点击图片保存。')
                    
                    wx.showToast({
                      title: '获取权限成功',
                      duration: 1000,
                      //  icon: 'none',
                      icon: 'success',
                      success: function () {
        
                        // _this.url =  `${getApp().globalData.webUrl}/wa/distribution/bangmai.html?goods_id=${_this.goods_id}&dealer_member_id=${_this.dealer_member_id}`;
                        setTimeout(function () {
                          wx.navigateBack({ changed: true });
                        }, 1500)
        
                      }
        
                    });
                  } else {
                    console.log('获取权限失败，不授权无法正常保存图片')
                    
                    wx.showToast({
                      title: '获取权限失败',
                      duration: 1000,
                      //  icon: 'none',
                      icon: 'success',
                      success: function () {
        
                        // _this.url =  `${getApp().globalData.webUrl}/wa/distribution/bangmai.html?goods_id=${_this.goods_id}&dealer_member_id=${_this.dealer_member_id}`;
                        setTimeout(function () {
                          wx.navigateBack({ changed: true });
                        }, 1500)
        
                      }
        
                    });
                  }
                }
              })
            }else{
              wx.navigateBack({ changed: true });
            }
          }
        })
      }
    });
    
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})